package com.njwb.util;

import com.njwb.mapper.RowMapping;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * 模板类
 * Author: yuandeyin@itany.com
 * Date: 2025-08-11 11:50
 * Version: 1.0
 */
public class JDBCTemplate {
    /**
     * 专门执行insert  delete  update
     * @param sql
     * @param params
     * @return
     */
    public static int executeUpdate(String sql,Object ... params){
        int rowCount = 0;
        //1.加载驱动
        //2.获取连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            //3.获取SQL执行器
            pstmt = conn.prepareStatement(sql);
            //占位符赋值
            if(null != params && params.length > 0){//说明params中有参数
                for(int i = 0; i < params.length; i++){
                    pstmt.setObject((i + 1),params[i]);
                }
            }
            //4.执行SQL
            rowCount = pstmt.executeUpdate();
            //5.反馈结果  return返回
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {//6关闭资源
            DBUtil.close(pstmt,conn);
        }
        return rowCount;
    }

    public static int insert(String sql,Object ... params){
        return executeUpdate(sql,params);
    }
    public static int delete(String sql,Object ... params){
        return executeUpdate(sql,params);
    }
    public static int update(String sql,Object ... params){
        return executeUpdate(sql,params);
    }

    /**
     * 统一封装查询
     * @param sql
     * @param params
     * @return List里面装什么？实体 可能是Dept ，也有可能是Brand
     *         List<T>泛型</>
     */
    public static <T> List<T> excuteQuery(String sql, RowMapping<T> rowMapping, Object ... params){
        //准备一个集合
        List<T> datas = new ArrayList<T>();
        //1.加载驱动
        //2.获取连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            //3.获取SQL执行器
            pstmt = conn.prepareStatement(sql);
            //赋值
            if(null != params && params.length > 0){
                for(int i = 0; i < params.length; i++){
                    pstmt.setObject((i + 1), params[i]);
                }
            }
            //4.执行SQL
            rs = pstmt.executeQuery();
            //5.数据的反馈
            //将数据库表中的每行数据转换成一个实体对象
            //存储到datas集合中
            while(rs.next()){
                //实体对象  对象 = new 实体类()
                //对象的setXX(rs.getXX())
                //datas.add(对象)
                //行数据转换成一个实体对象
                T t = rowMapping.mapper(rs);
                datas.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6.关闭资源
            DBUtil.close(rs,pstmt,conn);
        }
        return datas;
    }

    public static <T> List<T> selectList(String sql, RowMapping<T> rowMapping, Object ... params){
        return excuteQuery(sql,rowMapping,params);
    }

    public static <T> T selectOne(String sql, RowMapping<T> rowMapping, Object ... params){
        List<T> datas = selectList(sql,rowMapping,params);
        if(datas.size() == 0){
            return null;
        }else{
            return datas.get(0);
        }
    }
}
